home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / SAT 2.4.0 / SAT / Demo ƒ / HeartQuest demo ƒ / sBonus.p < prev    next >
Encoding:
Text File  |  1997-02-16  |  5.9 KB  |  250 lines  |  [TEXT/PJMM]

  1. {===============================================}
  2. {================= Bonus sprite unit ================}
  3. {===============================================}
  4.  
  5. { Example file for Ingemars Sprite Animation Toolkit. }
  6. { © Ingemar Ragnemalm 1992 }
  7. { See doc files for legal terms for using this code. }
  8.  
  9. unit sBonus;
  10.  
  11. { Sprite unit. A sprite unit should include the following routines:}
  12. { Init-procedure, that initializes private bitmaps}
  13. { Setup-procedure, that sets variables other than the standard ones set by MakeSprite }
  14. { Handle-procedure, to be called once per iteration until the sprite dies }
  15. { Hittask-procedure (optional), for advanced collission handling. }
  16.  
  17. interface
  18.  
  19.     uses
  20. {$IFC UNDEFINED THINK_PASCAL}
  21.         Types, Quickdraw, Fonts, QuickDrawText, Dialogs, MixedMode, Resources,
  22. {$ELSEC}
  23.         InterfacesUI, 
  24. {$ENDC}
  25.         SAT, MiscGraphics, sPoints, scores, SoundConst, GameGlobals;
  26.  
  27.     procedure InitBonus;
  28.     procedure SetupBonus (me: SpritePtr);
  29.     procedure HandleBonus (me: SpritePtr);
  30.     procedure SetStrings;
  31.  
  32. implementation
  33.  
  34.     var
  35.         bonusFace: array[1..3] of FacePtr;
  36.  
  37.     procedure BuildSweetFace (s1, s2: Str255);
  38.         var
  39.             fnum: Integer;
  40.             r, r2: Rect;
  41.             info: FontInfo;
  42.             s1w, s2w, maxWidth, theHeight: Integer;
  43.     begin
  44.         SetRect(r, 0, 0, 16, 16);
  45.         if bonusFace[3] = nil then
  46.             bonusFace[3] := SATNewFace(r);
  47.         SATSetPortFace(bonusFace[3]);            {I need a port to work in! The iconPort is ok.}
  48.  
  49.         GetFNum('Geneva', fnum);
  50.         TextFont(fnum);
  51.         TextSize(9);
  52.         TextFace([]);
  53.  
  54.         s1w := StringWidth(s1) + 4;
  55.         s2w := StringWidth(s2) + 4;
  56.  
  57.         if s1w > s2w then
  58.             maxWidth := s1w
  59.         else
  60.             maxWidth := s2w;
  61. {if maxWidth > 64 then}
  62. {maxWidth := 64;}
  63.         if maxWidth < 12 then
  64.             maxWidth := 12;
  65.  
  66.         GetFontInfo(info);
  67.         theHeight := (info.ascent + info.descent) * 2; {Fit 2 rows!}
  68.         if theHeight < 10 then
  69.             theHeight := 10;
  70.  
  71.         SATDisposeFace(bonusFace[3]);
  72.  
  73.         r.right := maxWidth;
  74.         r.bottom := theHeight;
  75.         bonusFace[3] := SATNewFace(r);
  76.  
  77.         SATSetPortFace(bonusFace[3]);
  78. {Font and size are already set - it is the same GrafPort, just new pixel data!}
  79.  
  80. {Fill with gray}
  81. {Draw frame with lines}
  82.         EraseRect(r);
  83.         if gSAT.initDepth > 1 then
  84.             begin
  85. {Medium-gray fill}
  86.                 RGBForeColor(MakeRGBColor($8000, $8000, $8000));
  87.                 PaintRect(r);
  88. {Dark-gray below}
  89.                 RGBForeColor(MakeRGBColor($4000, $4000, $4000));
  90.                 MoveTo(r.right - 1, r.top);
  91.                 LineTo(r.right - 1, r.bottom - 1);
  92.                 LineTo(r.left, r.bottom - 1);
  93. {Light-gray above}
  94.                 RGBForeColor(MakeRGBColor($C000, $C000, $C000));
  95.                 MoveTo(r.right - 1, r.top);
  96.                 LineTo(r.left, r.top);
  97.                 LineTo(r.left, r.bottom - 1);
  98.  
  99.                 ForeColor(blackColor);
  100.             end
  101.         else
  102.             FrameRect(r);
  103.  
  104. {Draw text on two lines}
  105.         r2 := r;
  106.         r2.top := r2.bottom div 2;
  107.         StringCenter(s2, r2, true, 0);
  108.         r2.bottom := r2.top;
  109.         r2.top := 0;
  110.         StringCenter(s1, r2, true, 0);
  111.  
  112.         SATSetPortMask(bonusFace[3]);
  113.         PaintRect(r);
  114.         SATChangedFace(bonusFace[3]);
  115.         SATSetPortScreen;
  116.     end; {BuildSweetFace}
  117.  
  118.  
  119.     procedure SetStrings;
  120.         var
  121.             d: DialogPtr;
  122.             itemHandle: Handle;
  123.             itemType, item: integer;
  124.             itemRect: Rect;
  125.             str: Str255;
  126.             itemHit: Integer;
  127. {$IFC GENERATINGPOWERPC }
  128.             filterProc: ProcPtr;
  129. {$ENDC}
  130.     begin
  131.         d := GetNewDialog(kSetStringsDialog, nil, WindowPtr(-1));
  132.  
  133.         GetDialogItem(d, 4, itemType, itemHandle, itemRect);
  134.         SetDialogItemText(itemHandle, features^^.sweetString1);
  135.         GetDialogItem(d, 5, itemType, itemHandle, itemRect);
  136.         SetDialogItemText(itemHandle, features^^.sweetString2);
  137.         SelectDialogItemText(d, 4, 0, 32767);
  138.  
  139. {$IFC GENERATINGPOWERPC }
  140.         filterProc := NewRoutineDescriptor(@Filter, uppModalFilterProcInfo, GetCurrentISA);
  141. {$ENDC}
  142.  
  143.         repeat
  144. {$IFC GENERATINGPOWERPC }
  145.             ModalDialog(filterProc, itemHit);
  146. {$ELSEC}
  147.             ModalDialog(@Filter, itemHit);
  148. {$ENDC}
  149.         until itemHit in [ok, cancel];
  150.  
  151.         if itemHit = ok then
  152.             begin
  153.                 GetDialogItem(d, 4, itemType, itemHandle, itemRect);
  154.                 GetDialogItemText(itemHandle, str);
  155.                 if length(str) > 15 then
  156.                     str := Copy(str, 1, 15);
  157.                 features^^.sweetString1 := str;
  158.  
  159.                 GetDialogItem(d, 5, itemType, itemHandle, itemRect);
  160.                 GetDialogItemText(itemHandle, str);
  161.                 if length(str) > 15 then
  162.                     str := Copy(str, 1, 15);
  163.                 features^^.sweetString2 := str;
  164.  
  165.                 ChangedResource(Handle(features));
  166.                 UpdateResFile(gPrefFile);
  167.  
  168.                 BuildSweetFace(features^^.sweetString1, features^^.sweetString2);
  169.             end;
  170.  
  171.         DisposeDialog(d);
  172.     end; {SetStrings}
  173.  
  174.  
  175.     procedure InitBonus;
  176.         var
  177.             ii: integer;
  178.             h: handle;
  179.     begin
  180.         for ii := 1 to 2 do
  181.             bonusFace[ii] := SATGetFace(127 + ii);
  182. {bonusFace[3] is built by the following call:}
  183.         BuildSweetFace(features^^.sweetString1, features^^.sweetString2);
  184.     end; {InitBonus}
  185.  
  186.     procedure SetupBonus (me: SpritePtr);
  187.         var
  188.             i: integer;
  189.     begin
  190.         i := SATRand(3) + 1;
  191.         me^.face := bonusFace[i];
  192.  
  193.         if me^.position.h < 300 then
  194.             me^.speed.h := 5
  195.         else
  196.             me^.speed.h := -5;
  197.  
  198. {Set the vertical speed depending on the icon.}
  199.         case i of
  200.             1: 
  201.                 me^.speed.v := 0;
  202.             2: 
  203.                 me^.speed.v := 10 * SATRand(2) - 5; {-5 or 5}
  204.             3: 
  205.                 me^.speed.v := 6 * SATRand(2) - 3; {-3 or 3}
  206.         end; {case}
  207.  
  208.         me^.hotRect := me^.face^.iconMask.bounds;
  209. {SetRect(me^.hotRect, -13 + 16, -28 + 32, 13 + 16, 0 + 32);}
  210.  
  211.         me^.task := @HandleBonus;
  212.     end; {SetupBonus}
  213.  
  214.     procedure HandleBonus (me: SpritePtr);
  215.         var
  216.             mp: Spriteptr;
  217.     begin
  218.         if me^.kind <> -4 then
  219.             begin
  220.                 me^.task := nil; { Caught by the player! }
  221.                 AddScore(50);
  222.                 mp := SATNewSprite(0, me^.position.h, me^.position.v, @SetupPoints);
  223.                 SATSoundPlay(jaSndH, 5, true);
  224.  
  225.                 bonusesCollected := bonusesCollected + 1;
  226.             end
  227.         else
  228.             begin
  229.                 bonusObjectRunning := true;
  230.                 if not features^^.macho then
  231.                     levelCompleted := false;
  232.             end;
  233.  
  234.         me^.mode := me^.mode + 1;
  235.  
  236.         me^.position.h := me^.position.h + me^.speed.h;
  237.         if me^.position.h > gSAT.offSizeH then
  238.             me^.task := nil;
  239.         if me^.position.h < -32 then
  240.             me^.task := nil;
  241.  
  242.         me^.position.v := me^.position.v + me^.speed.v;
  243.         if me^.position.v > gSAT.offSizeV - me^.face^.iconMask.bounds.bottom then
  244.             me^.speed.v := -Abs(me^.speed.v);
  245.         if me^.position.v < 0 then
  246.             me^.speed.v := Abs(me^.speed.v);
  247.  
  248.     end; {HandleBonus}
  249.  
  250. end.